<template>
  <!-- // 分配角色 -->
  <div>
    这里将来会放置多选列表
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="role in list" :key="role.id" :label="role.id">{{
        role.name
      }}</el-checkbox>
      <!-- <el-checkbox label="2">网抑云</el-checkbox>
      <el-checkbox label="运动" /> -->
    </el-checkbox-group>
    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSave">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles } from '@/api/settings.js'
import { getUserDetailById } from '@/api/user.js'
import { assignRoles } from '@/api/employees.js'
export default {
  props: {
    curId: { type: String, default: '' }
  },
  data () {
    return {
      roleIds: [],
      list: []
    }
  },
  created () {
    this.loadRoles()

    // this.loadUserDetailById()
  },
  methods: {
    async loadUserDetailById () {
      try {
        const res = await getUserDetailById(this.curId)
        console.log('getUserDetailById', res)
        // 当前用户的角色数据
        this.roleIds = res.data.roleIds
      } catch (err) {
        console.log('getUserDetailById', err)
      }
    },
    async loadRoles () {
      try {
        const res = await getRoles(1, 100)
        console.log('getRoles', res)
        this.list = res.data.rows// 保存数据
      } catch (err) {
        console.log('getRoles', err)
      }
    },
    async hSave () {
      try {
        const res = await assignRoles({ id: this.curId, roleIds: this.roleIds })
        console.log('assignRoles', res)
        // 保存数据
        this.$message.success('保存成功')

        this.$emit('cancel')
      } catch (err) {
        console.log('assignRoles', err)
      }
    },
    closeDialog () {
      // 通知父组件
      this.$emit('cancel')
    }
  }
}
</script>
